package com.hibernate.rcache.strategy;

import com.hibernate.rcache.region.RcacheEntityRegion;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.spi.CollectionRegion;
import org.hibernate.cache.spi.EntityRegion;
import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy;
import org.hibernate.cache.spi.access.EntityRegionAccessStrategy;
import org.hibernate.cache.spi.access.SoftLock;
import org.hibernate.cfg.Settings;

public class ReadWriteRcacheEntityRegionAccessStrategy implements EntityRegionAccessStrategy {


    private final RcacheEntityRegion region;
    private final Settings settings;

    public ReadWriteRcacheEntityRegionAccessStrategy(RcacheEntityRegion rcacheEntityRegion, Settings settings) {
        this.region = rcacheEntityRegion;
        this.settings = settings;
    }

    public EntityRegion getRegion() {
        return region;
    }

    public boolean insert(Object key, Object value, Object version) throws CacheException {
        return false;
    }

    public boolean afterInsert(Object key, Object value, Object version) throws CacheException {

        region.writeLock( key );
        try {
            Object item = region.get(key);
            if ( item == null ) {
                region.putandlock( key, value );
                return true;
            }
            else {
                region.putandlock( key, value );
                return false;
            }
        }
        finally {
            region.writeUnlock( key );
        }
    }

    public boolean update(Object key, Object value, Object currentVersion, Object previousVersion) throws CacheException {
        return false;
    }

    public boolean afterUpdate(Object key, Object value, Object currentVersion, Object previousVersion, SoftLock lock) throws CacheException {
        region.writeLock( key );
        try {
            Object item = region.get(key);
            if ( item == null ) {
                region.putandlock( key, value );
                return true;
            }
            else {
                region.putandlock( key, value );
                return false;
            }
        }
        finally {
            region.writeUnlock( key );
        }


    }

    public Object get(Object key, long txTimestamp) throws CacheException {



        return  region.get(key);
    }

    public boolean putFromLoad(Object key, Object value, long txTimestamp, Object version) throws CacheException {
        return false;
    }

    public boolean putFromLoad(Object key, Object value, long txTimestamp, Object version, boolean minimalPutOverride) throws CacheException {
        return false;
    }

    public SoftLock lockItem(Object key, Object version) throws CacheException {
        return null;
    }

    public SoftLock lockRegion() throws CacheException {
        return null;
    }

    public void unlockItem(Object key, SoftLock lock) throws CacheException {

    }

    public void unlockRegion(SoftLock lock) throws CacheException {

    }

    public void remove(Object key) throws CacheException {
        region.remove( key );
    }

    public void removeAll() throws CacheException {
        region.clear();
    }

    public void evict(Object key) throws CacheException {
        region.remove(key);
    }

    public void evictAll() throws CacheException {
        region.clear();
    }
}
